import type {
  CreateCrudOptionsProps,
  CreateCrudOptionsRet,
  CrudExpose,
} from '@fast-crud/fast-crud';

import type { FormRulesExt } from '#/api';
import type { DefLoginLogModel } from '#/api/devOperation/system/model/defLoginLogModel';
import type { DropMenu } from '#/components/dropdown';
import type { ActionEnum } from '#/enums/commonEnum';

import { ref } from 'vue';

import { useAccess } from '@vben/access';
import { $t } from '@vben/locales';

import { BaseLoginLogApi } from '#/api/basic/system/baseLoginLog';
import { DictEnum } from '#/enums/commonEnum';
import { PermCode } from '#/enums/perm';
import {
  backendDict,
  createdTimeColumn,
  deleteButton,
  indexColumn,
} from '#/plugins/fast-crud/common';

const { hasPermission } = useAccess();
const permCode = PermCode.basic.system.baseLoginLog;

export function createCrudOptions(
  props: CreateCrudOptionsProps<DefLoginLogModel.DefLoginLogResultVO, any>,
): CreateCrudOptionsRet<DefLoginLogModel.DefLoginLogResultVO> {
  const selectedRowKeys = ref<string[]>([]);

  return {
    crudOptions: {
      request: {
        pageRequest: BaseLoginLogApi.pageRequest,
        delRequest: BaseLoginLogApi.delRequest,
        infoRequest: BaseLoginLogApi.infoRequest,
      },
      actionbar: {
        buttons: {
          add: { show: false },
          ...deleteButton({
            crudExpose: props.crudExpose,
            selectedRowKeys,
            removeFn: BaseLoginLogApi.remove,
            role: permCode.delete,
          }),
        },
      },
      table: {
        striped: true,
        rowKey: 'id',
        rowSelection: {
          type: 'checkbox',
          selectedRowKeys,
          onChange: (changed: string[]) => {
            selectedRowKeys.value = changed;
          },
        },
      },
      rowHandle: {
        width: '120px',
        buttons: {
          edit: { show: false },
          view: { show: hasPermission(permCode.view) },
          remove: {
            show: hasPermission(permCode.delete),
          },
          copy: { show: false },
        },
      },
      columns: {
        ...indexColumn(props.crudExpose),
        requestIp: {
          title: $t('devOperation.system.defLoginLog.requestIp'),
          type: 'text',
          column: { width: 120 },
          search: { show: true },
        },
        nickName: {
          title: $t('devOperation.system.defLoginLog.nickName'),
          type: 'text',
          column: { width: 120 },
          search: { show: true },
        },
        username: {
          title: $t('devOperation.system.defLoginLog.username'),
          type: 'text',
          column: { width: 120 },
          search: { show: true },
        },
        status: {
          title: $t('devOperation.system.defLoginLog.status'),
          type: 'dict-select',
          dict: backendDict({ type: DictEnum.LoginStatusEnum }),
          column: { width: 120 },
          search: { show: true },
        },
        description: {
          title: $t('devOperation.system.defLoginLog.description'),
          type: 'text',
        },
        loginDate: {
          title: $t('devOperation.system.defLoginLog.loginDate'),
          type: 'text',
        },
        ua: {
          title: $t('devOperation.system.defLoginLog.ua'),
          type: 'textarea',
          column: { show: false },
        },
        browser: {
          title: $t('devOperation.system.defLoginLog.browser'),
          type: 'text',
          column: { show: false },
        },
        browserVersion: {
          title: $t('devOperation.system.defLoginLog.browserVersion'),
          type: 'text',
        },
        operatingSystem: {
          title: $t('devOperation.system.defLoginLog.operatingSystem'),
          type: 'text',
        },
        location: {
          title: $t('devOperation.system.defLoginLog.location'),
          type: 'text',
        },
        ...createdTimeColumn({}),
      },
    },
  };
}

export const frontRules = (
  _crudExpose: CrudExpose,
  _mode: ActionEnum,
): FormRulesExt => {
  return {};
};

export const clearList: DropMenu[] = [
  {
    text: '保留1个月',
    event: '1',
  },
  {
    text: '保留三个月',
    event: '2',
  },
  {
    text: '保留六个月',
    event: '3',
  },
  {
    text: '保留一年',
    event: '4',
  },
  {
    text: '保留一千条',
    event: '5',
  },
  {
    text: '保留一万条',
    event: '6',
  },
  {
    text: '保留三万条',
    event: '7',
  },
  {
    text: '保留十万条',
    event: '8',
  },
  {
    text: '清空所有',
    event: '9',
  },
];
